package com.yiyuwanglu.basecore.web.controller;

import com.mongodb.WriteResult;
import com.yiyuwanglu.basecore.dao.mongodb.MongoDBBaseDao;
import com.yiyuwanglu.test.entity.Student;
import com.yiyuwanglu.test.entity.User;
import net.sf.json.JSONObject;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Controller
@RequestMapping("/test")
public class TestController {
    @Resource(name = "mongoDBBaseDao")
    MongoDBBaseDao mongoDBBaseDao;
    @Resource
    private MongoTemplate mongoTemplate;

    @ResponseBody
    @RequestMapping("/add")
    public Object testAdd(Model model) {
        User user = new User();
        Date creatTime = new Date();
        user.setCreateTime(creatTime);
        user.setAge(10);
        user.setUsername("黄江南");
        mongoDBBaseDao.saveOrUpdate(user);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("user", user.toString());
        System.out.println(user.toString());
        return jsonObject;
    }

    @ResponseBody
    @RequestMapping("/query")
    public Object testFind() {
        Query query = Query.query(Criteria.where("age").gt(10));
        List<User> list = mongoTemplate.find(query, User.class, "user");
        return list;
    }

    @ResponseBody
    @RequestMapping("/update")
    public Object testUpdate(String id,Integer age, String username) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(new ObjectId(id)));
        Update update = new Update();
        update.set("age", age);
        update.set("username", username);
        WriteResult result = mongoTemplate.updateFirst(query, update, "user");
        return result;
    }


    @ResponseBody
    @RequestMapping("/deleteById")
    public Object delete(String id){
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(new ObjectId(id)));
        mongoTemplate.remove(query,"user");
        return "success";
    }

    /**
     * "5e49eb8d837e031542d6a169"http://localhost:8080/abc/test/queryById?id=5e49eb8d837e031542d6a169
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryById")
    public Object findById(String id) {
        User re = mongoTemplate.findById(id, User.class,"user");
        return re;
    }

    @ResponseBody
    @RequestMapping("/addStudnet")
    public Object testAddStudent(Integer count) {
        Random random = new Random();
        for (int i = 0; i < count; i++) {
            Student student = new Student();
            student.setId(UUID.randomUUID().toString().replace("-", ""));
            student.setAge(random.nextInt(80) + 18);
            student.setAddress("青陽");
            student.setName("金旭東");
            mongoTemplate.save(student, "student");
        }
        return "success";
    }


    public static void main(String[] args) {
        System.out.println(new ObjectId("222222222"));
    }
}
